#!/bin/sh
#
# AUTHOR: 	Timothy J. Luoma
#
# EMAIL: 	luomat@capitalist.princeton.edu
# 		476tjl@ptsmail.ptsem.edu
#
# DATE: 	21 Nov 1995
#
# WARRANTY:	"  " (this space blank on purpose)
#
# PURPOSE:	Allows (root) user to eject a disk at the
#	 	command-line.
#
# RULES FOR USE: Use at your own risk.  This has only been tested
# by me under limited, specific usage.  Don't charge someone else
# money for it.  If you improve it, let me know.  If you redistribute
# it after improving it, please keep my name on their somewhere,
# even if just to say "This idiot did this all wrong, so I fixed
# it."
#
# SOME USAGE NOTES:
# 1) Some error messages occur when I am eject a disk using this
# script, but none of them seem to harm anything.
# 
# 2) Usage: both 'umount' and 'disk -e' must be run by 'root',
# unless root changes the execute permissions.  Sorry.  There are
# obvious workarounds (SETUID root/chmod 4755 is what I did since
# I'm the only user on my NeXTStation).
#
# 3) The 'umount' and 'disk -e' commands are done on ONE line,
# connected by "&&" so that the 'disk -e' is done ONLY when the
# 'umount' has already been done.  This keeps the disk from being
# ejected when it hasn't been umounted, which can be a real pain
# in the compiler.
#
# 4) I use 'umount -v' which echoes when the disk is unmounted.
# You can remove the '-v' if you don't like this.
#
# 5) If the script complains about not being able to find the
# 'mount' command, either use the full path (usually /usr/etc/mount)
# or add the foldername to the $PATH (but you knew that, didn't
# you ;-)
#  
# 6) This script assumes that your floppy disks are mounted on
# '/dev/rfd0b' because this is where my 3.2 NeXTStation mounts
# floppy disks.
#
# 7) CDs seem to be mounted in a way that makes little sense to me.
# My CD drive is /dev/sd2a.  Some CDs are mounted on /dev/sd2a
# (most notably the NeXT CDs), and some are mounted on /dev/rsd2h.
# To account for this, I simply defined it as if there was an
# additional drive which gets mounted on 'rsd2h'.
#
# 8) ONE VERY important note: to eject a disk using 'disk -e' you
# need to reference the RAW device.  That is, for /dev/sd2a I need
# to use /dev/rsd2a.  TO THAT END: When you define the drive
# variables, you must use the regular device.  THEN when you use
# 'disk -e' you must use 'disk -e /dev/r$yourvariablename'
# "WHY?" you ask.  Because 'mount' reports the regular device,
# while 'disk -e' requires the raw device.  Hey, this is a hack,
# after all.
#
# 9) IF you can install another drive, I'm quite sure that you can
# figure out how to add the eject code for it to this simple script.
# I hope I have given a sufficiently useful road map on how this
# can be done.
#
# 10) This entire script is a hack.  It isn't meant to be particularly
# pretty, it's just meant to allow you to eject a disk from the
# commandline.
#
#
# 11) Oh, once you have read this, you can remove the next few lines and
# make the program actually work:
# echo "You should read through the code first to make sure that you"
# echo "know how this program works and what assumptions it operates"
# echo "under or else it might not work, or not work how you think "
# echo "it should work.    USE AT YOUR OWN RISK!!!!"
# exit 0
# end of sermon about reading the code before running the program.





name=`basename $0`

	##########################################################
	# The FORMAT is important here.  It is simply the part   #
	# which comes after "/dev/".  If you don't know it is,   #
	# check 'mount -p' and look at the first field.  Example:#
	# my floppy drive is /dev/rfd0b, so I use "rfd0b"        #
	##########################################################

# This is where my floppies get mounted
floppydrive=rfd0b
floppy2=fd0a

# this is where my NeXT CDs get mounted
# NOTE:	this is not the RAW device, 
# 	see BELOW for how this is handled. 
# 	see ABOVE for why this is a problem.
otherdrive=sd2a

# This is where my non-NeXT CDs get mounted
yadrive=rsd2h


####################################
# the actual program begins here.  #
####################################

if [ $# -lt 1 ]
then 
	echo "<Usage> $name /diskname"
	exit 0
fi



	# check the mountpoint, ie, which /dev it is mounted on.
	# This is the first field in the 'mount -p' command.
mounted=`mount -p | grep $1 | awk '{print $1}' | sed 's/\/dev\///'`



	##########################################################
	# If the mountpoint is the floppydrive, eject the floppy #
	# drive                                                  #
	##########################################################
	
if [ "$mounted" = "$floppydrive" ]
then
	 umount -v $1 && \
	 disk -e /dev/$floppydrive 


elif [ "$mounted" = "$floppy2" ]
then
	 umount -v $1 && \
	 disk -e /dev/r$floppy2

	##########################################################
	# 	        	        	        	 #
	# NOTE: This is a SPECIAL CASE.  You need to use the RAW #
	# device here.     	        	        	 #
	# 	        	        	        	 #
	##########################################################

elif [ "$mounted" = "$otherdrive" ]
then
	 umount -v $1 && \
	 disk -e /dev/r$otherdrive
	     # note the "r" ^



	##########################################################
	# if the mountpoint is the otherdrive, eject that.       #
	##########################################################

elif [ "$mounted" = "$yadrive" ]
then
	 umount -v $1 && \
	 disk -e /dev/$yadrive




	############################################################
	# if the mountpoint is none of the above, don't try to     #
	# eject it... You'd hate to dismount something by accident,#
	# like "/" ;-)                                             #
	############################################################

else
	echo "$name error: not a disk"
fi


####################################################################
# If you have TickleServices, then you have a command called
# "WSNotify" which updates the File Viewer from the command-line,
# much like "Update Viewer" in the WM menu.  If you have it, this
# would be a good place to invoke it, so the File Viewer will know
# that the disk has been ejected.

# NOTE: WSNotify only seems to work for me if the File Viewer is
# not hidden.  Your mileage may vary ;-)

# If you have WSNotify, uncomment the next line. 
WSNotify && echo "$name: Updated File Viewer"

exit 0

